package com.dure.hotmemory.scenes;

import android.Manifest;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.os.Handler;

import com.dure.hotmemory.bean.Cell;
import com.dure.hotmemory.controllers.WelcomeController;
import com.dure.hotmemory.drawview.BaseSurfaceView;
import com.socks.library.KLog;

import java.util.ArrayList;

/**
 * Created by dure on 2017/6/12.
 */

public class WelcomeScene extends BaseScene {
    Paint paint;
    float frameWidth, frameHeight;
    float cellWidth, cellHeight;
    RectF frameRectF;
    ArrayList<Cell> cellList = new ArrayList<>();
    boolean dataPrepared, welcomeStart, welcomeEnd;
    private long startTime;
    WelcomeController controller;
    public Handler handler = new Handler();
    private EmbossMaskFilter emboss;

    public WelcomeScene(BaseSurfaceView baseSurfaceView) {
        super(baseSurfaceView);
        paint = new Paint();
        paint.setAntiAlias(true);
        controller = new WelcomeController(this);
        controller.checkPermission(Manifest.permission.READ_PHONE_STATE, 0, null);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
            float[] direction = new float[] { 1, 1, 1 };
            // 设置环境光亮度
            float light = 0.5f;
            // 选择要应用的反射等级
            float specular = 10;// 这个参数更改没有看出效果上面的变化
            // 向mask应用一定级别的模糊
            float blur = 3;// 这个参数可以控制立体侧面的宽度
            emboss = new EmbossMaskFilter(direction, light, specular, blur);
            paint.setMaskFilter(emboss);
        }


        dataInited = true;
    }

    @Override
    public void handleData() {
        if (!dataInited) {
            return;
        }

        if (!dataPrepared) {
            dataPrepared = true;
            getCellList();
            startTime = System.currentTimeMillis();
            welcomeStart = true;
        }

        if (welcomeStart && System.currentTimeMillis() - startTime > 3000) {
            welcomeStart = false;
            startTime = System.currentTimeMillis();
            welcomeEnd = true;
        }

        if (welcomeEnd && System.currentTimeMillis() - startTime > 2000) {
            welcomeEnd = false;
            startTime = System.currentTimeMillis();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    activity.moveToNewScene(new MainScene(baseSurfaceView));
                }
            });
        }
    }

    private void getCellList() {
        cellList.clear();

        frameHeight = frameWidth = baseSurfaceView.getWidth() * 0.5f;
        float padding = frameWidth * 0.1f;
        cellHeight = cellWidth = frameWidth * 0.7f / 2;
        int centerX = baseSurfaceView.getWidth() / 2;
        int centerY = baseSurfaceView.getHeight() / 2;
        float frameLeft = centerX - frameWidth / 2;
        float frameTop = centerY - frameHeight / 2;
        float frameRight = centerX + frameWidth / 2;
        float frameBottom = centerY + frameHeight / 2;
        frameRectF = new RectF(frameLeft, frameTop, frameRight, frameBottom);

        for (int i = 0; i < 4; i++) {
            int columnIndex = i % 2;
            int rowIndex = i / 2;
            float left = frameLeft + columnIndex * cellWidth + (columnIndex + 1) * padding;
            float top = frameTop + rowIndex * cellHeight + (rowIndex + 1) * padding;
            float right = left + cellWidth;
            float bottom = top + cellHeight;
            Cell cell = new Cell();
            cell.rectF = new RectF(left, top, right, bottom);
            cellList.add(cell);

            if (i == 0 || i == 3) {
                cell.show = true;
            }
        }

//        int randomShowCount = 0;
//        while (randomShowCount < 2){
//            int randomIndex = (int) (Math.random() * 4);
//            Cell cell = cellList.get(randomIndex);
//            if (!cell.show){
//                cell.show = true;
//                randomShowCount++;
//            }
//        }
    }

    @Override
    public void handleDraw(Canvas canvas) {
        if (!dataInited) {
            return;
        }

        if (!dataPrepared)
            return;

        drawCells(canvas);
    }

    private void drawCells(Canvas canvas) {
        paint.setColor(Color.WHITE);
        float arc = frameWidth / 15f;
//        paint.setMaskFilter(new BlurMaskFilter(frameRectF.width() * 0.1f, BlurMaskFilter.Blur.OUTER));
        canvas.drawRoundRect(frameRectF, arc, arc, paint);
//        paint.setMaskFilter(null);

        for (int i = 0; i < cellList.size(); i++) {
            Cell cell = cellList.get(i);
            if (welcomeStart) {
                if (cell.show) {
//                    paint.setMaskFilter(new BlurMaskFilter(cell.rectF.width() * 0.05f, BlurMaskFilter.Blur.SOLID));
                    paint.setColor(Color.CYAN);
                } else {
                    paint.setColor(Color.GRAY);
                }
            } else {
                paint.setColor(Color.GRAY);
            }


            canvas.drawRect(cell.rectF, paint);
//            paint.setMaskFilter(null);
        }
    }
}
